package com.leyou.cart.service;

import com.leyou.auth.pojo.UserInfo;
import com.leyou.cart.interceptors.LoginInterceptor;
import com.leyou.cart.pojo.Cart;
import com.leyou.common.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CartService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    static final String KEY_PREFIX = "ly:cart:uid:";

    //先判断是否加入过，没有加入过，直接加入，已经加入过，改值
    public void addCart(Cart cart) {

        //从拦截器中取出登陆的用户
        UserInfo loginUser = LoginInterceptor.getLoginUser();


        //根据用户的id获取当前用户的所有的购物车信息，得到的是一堆的json
        BoundHashOperations<String, Object, Object> ops = redisTemplate.boundHashOps(KEY_PREFIX + loginUser.getId());

        //根据skuId获取对应的sku信息
        Object o = ops.get(cart.getSkuId() + "");

        if (null == o) {//如果o为空，说明没有加入过，没有加入过不牵扯修改值的问题，直接保存
            ops.put(cart.getSkuId() + "", JsonUtils.serialize(cart));
        } else {
            //把json对象还原成java pojo对象
            Cart storeCart = JsonUtils.parse(o.toString(), Cart.class);
            storeCart.setNum(storeCart.getNum() + cart.getNum());
            ops.put(cart.getSkuId() + "", JsonUtils.serialize(storeCart));
        }
    }

    public List<Cart> queryCart() {
        //根据用户的id获取redis中所有的购物车对象
        //从拦截器中取出登陆的用户
        UserInfo loginUser = LoginInterceptor.getLoginUser();


        //根据用户的id获取当前用户的所有的购物车信息，得到的是一堆的json
        BoundHashOperations<String, Object, Object> ops = redisTemplate.boundHashOps(KEY_PREFIX + loginUser.getId());


        List<Object> values = ops.values();


        List<Cart> carts = new ArrayList<>();
        if (values != null && 0!=values.size()) {

            for (Object value : values) {
                //把查到的json对象转为Cart对象
                carts.add(JsonUtils.parse(value.toString(),Cart.class));
            }
        }

        return carts;
    }

    public void incrementCart(Cart cart) {
        UserInfo loginUser = LoginInterceptor.getLoginUser();


        //根据用户的id获取当前用户的所有的购物车信息，得到的是一堆的json
        BoundHashOperations<String, Object, Object> ops = redisTemplate.boundHashOps(KEY_PREFIX + loginUser.getId());

        //取到对应的sku，从json转成对象
        Cart storeCart = JsonUtils.parse(ops.get(cart.getSkuId() + "").toString(),Cart.class);

        //修改对象的数量
        storeCart.setNum(storeCart.getNum()+1);

        //把修改后的对象重新存储
        ops.put(storeCart.getSkuId()+"",JsonUtils.serialize(storeCart));

    }

    public void DeleteCart(Long skuId) {

        UserInfo loginUser = LoginInterceptor.getLoginUser();


        //根据用户的id获取当前用户的所有的购物车信息，得到的是一堆的json
        BoundHashOperations<String, Object, Object> ops = redisTemplate.boundHashOps(KEY_PREFIX + loginUser.getId());

        ops.delete(skuId+"");
    }
}
